package com.zzjson.serial;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.serial</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月20日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class XStreamSerializer implements ISerializer {

	XStream xStream = new XStream(new DomDriver());

	@Override
	public <T> byte[] serialize(T obj) {
		return xStream.toXML(obj).getBytes();
	}

	@Override
	public <T> T deserialize(byte[] data, Class<T> clazz) {
		return (T) xStream.fromXML(new String(data));
	}

	public static void main(String[] args) {
		User user = new User();
		user.setAge(18);
		user.setName("zzy");
		ISerializer iSerializer = new XStreamSerializer();
		byte[] serialize = iSerializer.serialize(user);
		System.out.println("byte.length: " + serialize.length);
		User user2 = iSerializer.deserialize(serialize, User.class);
		System.out.println(user2);

	}
}
